import store from '../store/index'
import axios from 'axios'
/*
 * @Author: WangXiao
 * @Date: 2021-04-08 20:47:39
 * @LastEditTime: 2021-04-18 09:17:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /dvr-ui/src/methods/videoPlay.js
 */

/**
  * @description: (表单第一部分)创建视频基本信息表单
  * @param {*} formName 不用修改
  * @return {*}
*/
let creatForm = function (formName) {
  let _this = this
  this.$refs[formName].validate((valid) => {
    if (valid) {//提交符合规则时
      console.log(this.$refs[formName].model);
      //发送 post 请求
      axios({
        headers: {
          'Content-type': 'charset=UTF-8'
        },
        method: 'post',
        url: 'http://localhost:8000/video/uploadVI',
        data: this.$refs[formName].model,

      }).then(function (response) {
        console.log(response)
        _this.vid = response.data
        store.state.vid = response.data
        _this.$message({
          message: '视频创建成功',
          type: 'success'
        });
        _this.vUptag++
      }).catch(function () {
        _this.$notify.error({
          title: '错误',
          message: '创建数据库失败，可能是标题重复或者服务器问题'
        });
      })
    }
  });
}

/**
 * @description: 功能选择
 * @param {*} key 当前标签页
 * @param {*} keyPath 跟标签页
 * @return {*}
 */
let handleSelect = function (key) {
  //console.log("当前的页面号为 key = " + key)
  //修改全局变量
  store.state.count = key
  //console.log("全局变量测试:" + store.state.count)
  // if (this.mvTotal == 0) {//用于测试显示下方进度条
  //   this.mvTotal++;
  // } else {
  //   this.mvTotal--;
  // }
  //TODO:换页下方页面替换

}

/**
 * @description: 点击文件叉子后移除文件进行的动作
 * @param {*} file
 * @param {*} fileList
 * @return {*}
 */
let handleRemove = function (file, fileList) {
  console.log(file, fileList);
}
/**
 * @description: 点击文件时调用的钩子
 * @param {*} file
 * @return {*}
 */
let handlePreview = function (file) {
  console.log(file);
}

/**
 * @description:  超过所选数
 * @param {*} files
 * @param {*} fileList
 * @return {*}
 */
let handleExceed = function (files, fileList) {
  this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
}
/**
 * @description : 成功钩子
 * @param {*} file 
 * @param {*} fileList 
 * @returns {*}
 */
let handleSuccess = function (response, file, fileList) {
  this.flag = '1' //上传视频成功后，就可以提交表单了
  console.log(response, file, fileList)
}
/**
 * 
 * @param {*} file 
 * @param {*} fileList 
 */
let handleError = function (err, file, fileList) {
  console.log(err, file, fileList)
}
/**
 * 
 * @description 上传视频时判断是否为视频
 * @param {*} file 
 * @param {*} fileList 
 */
let handleBeforeUpload = function (file) {
  let arr = file.name.split(".")
  let fileType = arr[arr.length - 1];
  if (fileType != "flv" & fileType != "mp4" & fileType != "mpg") {
    this.$alert('只支持上传flv,mp4,mpg格式的视频', '错误', {
      confirmButtonText: '确定',
      callback: () => {
        this.$notify.error({
          title: '错误',
          message: '上传失败'
        });
      }
    });
    return false;
  }
}
export { creatForm, handleSelect, handleRemove, handlePreview, handleExceed, handleSuccess, handleError, handleBeforeUpload }